﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;

namespace FarmSmartSleep.Organ.API.Application.SleepInfoCommands.RecordCommands
{
    public class SleepRecordSearchHandler : IRequestHandler<SleepRecordSearchCommand, ResultT<SleepRecordDto>>
    {
        //栋舍
        private readonly IBaseRepository<Cottages> cottagebaseRepository;
        //栏位
        private readonly IBaseRepository<Fields> fieldsRepository;
        //羊场
        private readonly IBaseRepository<SheepFarm> farmbaseRepository;
        //品种
        private readonly IBaseRepository<Variety> VarietybaseRepository;
        //基因等级
        private readonly IBaseRepository<GeneGrade> geneGradeRepository;
        //羊只类型
        private readonly IBaseRepository<SleepType> sleeptypebaseRepository;

        //档案信息
        private readonly IBaseRepository<SleepInfor> sleepinforbaseRepository;
        //出生状态
        private readonly IBaseRepository<BirthStatus> birthstatubaseRepository;
        //栋舍类型
        private readonly IBaseRepository<CottageType> cottageTypeRepository;



        public SleepRecordSearchHandler(IBaseRepository<Cottages> cottagebaseRepository, IBaseRepository<Variety> varietybaseRepository,
                                        IBaseRepository<GeneGrade> geneGradeRepository,
                                        IBaseRepository<Fields> fieldsRepository, IBaseRepository<FemaleBegin> femaleBeginRepository,
                                        IBaseRepository<SleepInfor> sleepinforbaseRepository,
                                        IBaseRepository<BirthStatus> birthstatubaseRepository, IBaseRepository<SheepFarm> farmbaseRepository,
                                        IBaseRepository<SleepType> sleeptypebaseRepository, IBaseRepository<CottageType> cottageTypeRepository)

        {
            this.cottagebaseRepository = cottagebaseRepository;
            this.VarietybaseRepository = varietybaseRepository;
            this.geneGradeRepository = geneGradeRepository;
            this.fieldsRepository = fieldsRepository;
            this.sleepinforbaseRepository = sleepinforbaseRepository;
            this.birthstatubaseRepository = birthstatubaseRepository;
            this.farmbaseRepository = farmbaseRepository;
            this.sleeptypebaseRepository = sleeptypebaseRepository;
            this.cottageTypeRepository = cottageTypeRepository;

        }
        /// <summary>
        /// 羊只档案
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultT<SleepRecordDto>> Handle(SleepRecordSearchCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultT<SleepRecordDto>() { Code = (int)CodeEnum.search };
            List<SleepRecordDto> res = new List<SleepRecordDto>();
             res = (from a in sleepinforbaseRepository.GetAll()
                        join b in farmbaseRepository.GetAll() on a.FarmId equals b.FarmId
                        join c in cottagebaseRepository.GetAll() on a.RecordcottageId equals c.CottagesId
                        join d in fieldsRepository.GetAll() on a.FieIdId equals d.FieIdId
                        join e in sleeptypebaseRepository.GetAll() on a.SleepTypeId equals e.SleepTypeId
                        //join f in birthstatubaseRepository.GetAll() on a.BirthdayId equals f.BirthdayId
                        join g in VarietybaseRepository.GetAll() on a.VarietyId equals g.VarietyId
                        join h in geneGradeRepository.GetAll() on a.GradeId equals h.GradeId
                        join i in cottageTypeRepository.GetAll() on c.CottageTypeId equals i.CottageTypeId

                        select new SleepRecordDto()
                        {
                            SleepInforId = a.SleepInforId,
                            EarNumber = a.EarNumber,
                            VarietyId = a.VarietyId,
                            ElectronicEarTag = a.ElectronicEarTag,
                            GradeId = a.GradeId,
                            SheepImage = a.SheepImage,
                            Gender = a.Gender,
                            SleepTypeId = a.SleepTypeId,
                            StayStatus = a.StayStatus,
                            FarmId = a.FarmId,
                            RecordcottageId = a.RecordcottageId,
                            FieIdId = a.FieIdId,
                            FarEarNumber = a.FarEarNumber,
                            MoEarNumber = a.MoEarNumber,
                            SleepBirthDate = a.SleepBirthDate,
                            SleepEntryDate = a.SleepEntryDate,
                            SleepBirthPlace = a.SleepBirthPlace,
                            SleepBirthWeight = a.SleepBirthWeight,
                            SleepNotes = a.SleepNotes,
                            BirthdayId = a.BirthdayId,
                            SleepTypeName = e.SleepTypeName,
                            //BirsdayName = f.BirsdayName,
                            Grade = h.Grade,
                            CottageTypeId = i.CottageTypeId,
                            CottageName = c.CottageName,
                            FieIdName = d.FieIdName,
                            BreedStatuId = a.BreedStatuId,
                            BreedDayNum = a.BreedDayNum,
                            CheckPre = a.CheckPre,
                            ProductLevel = a.ProductLevel,
                            BuyLevel = a.BuyLevel,
                            WeanWeight = a.WeanWeight,
                            WeanData = a.WeanData,
                            //AgeDay = (DateTime.Now.Day - a.SleepBirthDate.Day)+ (DateTime.Now.Month - a.SleepBirthDate.Month)*30,
                       
                            agetotal = (int)(DateTime.Now- Convert.ToDateTime( a.SleepBirthDate)).TotalDays,
                            VarietyName = g.VarietyName,
                        }).ToList()
                     .Union(
                from a in sleepinforbaseRepository.GetAll()
                join b in farmbaseRepository.GetAll() on a.FarmId equals b.FarmId
                //join c in cottagebaseRepository.GetAll() on a.RecordcottageId equals c.CottagesId
                //join d in fieldsRepository.GetAll() on a.FieIdId equals d.FieIdId
                join e in sleeptypebaseRepository.GetAll() on a.SleepTypeId equals e.SleepTypeId
                //join f in birthstatubaseRepository.GetAll() on a.BirthdayId equals f.BirthdayId
                join g in VarietybaseRepository.GetAll() on a.VarietyId equals g.VarietyId
                join h in geneGradeRepository.GetAll() on a.GradeId equals h.GradeId
                //join i in cottageTypeRepository.GetAll() on c.CottageTypeId equals i.CottageTypeId
                where a.RecordcottageId==null
                select new SleepRecordDto()
                {
                    SleepInforId = a.SleepInforId,
                    EarNumber = a.EarNumber,
                    VarietyId = a.VarietyId,
                    ElectronicEarTag = a.ElectronicEarTag,
                    GradeId = a.GradeId,
                    SheepImage = a.SheepImage,
                    Gender = a.Gender,
                    SleepTypeId = a.SleepTypeId,
                    StayStatus = a.StayStatus,
                    FarmId = a.FarmId,
                    RecordcottageId = a.RecordcottageId,
                    FieIdId = a.FieIdId,
                    FarEarNumber = a.FarEarNumber,
                    MoEarNumber = a.MoEarNumber,
                    SleepBirthDate = a.SleepBirthDate,
                    SleepEntryDate = a.SleepEntryDate,
                    SleepBirthPlace = a.SleepBirthPlace,
                    SleepBirthWeight = a.SleepBirthWeight,
                    SleepNotes = a.SleepNotes,
                    BirthdayId = a.BirthdayId,
                    SleepTypeName = e.SleepTypeName,
                    //BirsdayName = f.BirsdayName,
                    Grade = h.Grade,
                    //CottageTypeId = i.CottageTypeId,
                    //CottageName = c.CottageName,
                    //FieIdName = d.FieIdName,
                    BreedStatuId = a.BreedStatuId,
                    BreedDayNum = a.BreedDayNum,
                    CheckPre = a.CheckPre,
                    ProductLevel = a.ProductLevel,
                    BuyLevel = a.BuyLevel,
                    WeanWeight = a.WeanWeight,
                    WeanData = a.WeanData,
                    agetotal = (int)(DateTime.Now - Convert.ToDateTime(a.SleepBirthDate)).TotalDays,
                    VarietyName = g.VarietyName
                }).ToList();
            result.Data = res;


            return result;

        }
    }
}
